package com.fleapx.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fleapx.pojo.User;
import com.fleapx.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	public UserService userService;
	
	@RequestMapping("/{page}")
	public String toAdd(@PathVariable String page){
		return page;
	}
	
	@RequestMapping("/adduser")
	public String adduser(User user){
		userService.addUser(user);
		return "redirect:/user/getListUser";
	}
	
	@RequestMapping("/getListUser")
	public String getList(Model model){
		model.addAttribute("list", userService.getList());
		return "showUsers";
	}
	
	@RequestMapping("/toUpdate")
	public String toUpdate(int id,Model model){
		model.addAttribute("user", userService.getUserBykey(id));
		return "update";
	}
	@GetMapping
	public String index(){
		return "input";
	}

	@RequestMapping("/update")
	public String update(User user){
		userService.update(user);
		return "redirect:/user/getListUser";
	}
	
	@RequestMapping("/deleteUser")
	public String delete(int userId){
		userService.delete(userId);
		return "redirect:/user/getListUser";
	}

	//复制列表中最后一条数据  用于测试存储过程的读写分离环境下有没有问题  sharding jdbc无法执行存储过程
	@RequestMapping("/copyLastData")
	public String copyLastData(String name){
		System.out.println("name="+name);
		userService.copyLastData();
		return "redirect:/user/getListUser";
	}
	
	
}
